package com.xs.ahoi.hcxs.handler;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import com.xs.ahoi.hcxs.Ahoi;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.model.vo.DownloadProgressVO;
import com.xs.ahoi.hcxs.utils.LogUtil;
import com.xs.ahoi.hcxs.utils.StringUtils;

/**
 * 下载进度通知
 *
 * @author fuhuichao
 * @date 2020-07-14 20:51
 */
public class NotifyProgressHandler extends BaseNotifyHandler {

    private String channel_id_progress;
    private String channel_name_progress;
    private int NOTIFY_ID_PROGRESS;
    private Bitmap large_icon = null;

    // 初始化
    public void init(String fileName) {
        NOTIFY_ID_PROGRESS = BaseNotifyHandler.getNewNotifyId();
        LogUtil.e("NOTIFY_ID_PROGRESS:" + NOTIFY_ID_PROGRESS);
        channel_id_progress += Ahoi.getInstance().getPackageName() + "_" + fileName + "_" + NOTIFY_ID_PROGRESS;
        channel_name_progress += Ahoi.getInstance().getPackageName() + "_name_" + fileName + "_" + NOTIFY_ID_PROGRESS;

        Context context = Ahoi.getInstance().getApplicationContext();
        large_icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);

        // 创建渠道
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notificationManager.deleteNotificationChannel(channel_id_progress);
            NotificationChannel channelProgress = new NotificationChannel(channel_id_progress, channel_name_progress, NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(channelProgress);
        }
    }

    // 更新下载进度
    public void pushNotify(DownloadProgressVO progressVO) {

        String contentTitle = progressVO.getFileName();
        if (contentTitle.length() > 10) {
            contentTitle = contentTitle.substring(0, 10) + "...";
        }
        if (StringUtils.isNotEmpty(progressVO.getTotalSize())) {
            contentTitle = progressVO.getTotalSize() + contentTitle;
        }

        // 发起通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(Ahoi.getInstance().getApplicationContext(), channel_id_progress)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(large_icon)
                .setPriority(NotificationManager.IMPORTANCE_MAX)
                .setWhen(System.currentTimeMillis())
                .setOngoing(true)
                .setContentTitle(contentTitle)
                .setContentText(progressVO.getProgressText() + " - " + progressVO.getProgress() + "%")
                .setProgress(100, progressVO.getProgress(), false)
                .setDefaults(NotificationCompat.DEFAULT_SOUND)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

        if (progressVO.isOver()) {
            builder.setOngoing(false).setAutoCancel(true).setContentText(progressVO.getProgressText());
        }

        Notification notify = builder.build();
        notificationManager.notify(NOTIFY_ID_PROGRESS, notify);
    }
}
